package com.fps.web.controller.baseattach;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fps.web.model.Baseattach;
import com.fps.web.service.BaseattachService;
import com.fpswork.core.model.ExcelEntity;
import com.fpswork.core.model.PageEntity;
import com.fpswork.util.StringUtil;
import com.fps.web.controller.BaseController;

/**
 * 附件页面
 *
 */
@Controller
@RequestMapping("/baseattach")
public class BaseattachController extends BaseController{
    
    @Autowired
    BaseattachService baseattachService;
    
    
 
   
    /**
	 * 加载List数据
	 */
    @RequestMapping(value = "/loadBaseattachList", method = RequestMethod.GET)
    public @ResponseBody PageEntity loadBaseattachList(PageEntity pageEntity) throws Exception{
    	baseattachService.queryBaseattachList(pageEntity);
        return pageEntity;
    }
    
    
    
    /**
   	 * 附件管理
   	 */
    @RequestMapping("/baseattachManagement")
    public ModelAndView baseattachManagement(String id,String pagetype) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();   
    	Baseattach baseattach= new Baseattach();
    	if(!"add".equals(pagetype)){
    		baseattach = baseattachService.queryBaseattachByID(id);
    	}
    	modelAndView.addObject("baseattach",baseattach);
    	modelAndView.addObject("pagetype", pagetype);
		modelAndView.setViewName("web/baseattach/baseattachmanagement");      
        return modelAndView;
    }
    
    /**
   	 * 附件管理新增表单
   	 */
    @RequestMapping(value = "/baseattachAddSubmit", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> baseattachAddSubmit(@ModelAttribute("Baseattach")Baseattach baseattach) throws Exception{
    	baseattachService.addBaseattach(baseattach);
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("flag", "true");
        return map;
    }
     
    /**
   	 * 附件管理修改表单
   	 */
    @RequestMapping(value = "/baseattachUpdateSubmit", method = RequestMethod.POST)
    public @ResponseBody Baseattach baseattachUpdateSubmit(@ModelAttribute("Baseattach")Baseattach baseattach) throws Exception{
    	baseattachService.updateBaseattach(baseattach);
        return baseattach;
    }
    
    
    /**
   	 * 附件删除
   	 */
    @RequestMapping(value = "/deleteBaseattach", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> deleteBaseattach(@ModelAttribute("Baseattach")Baseattach baseattach) throws Exception{
    	baseattachService.deleteBaseattach(baseattach);
    	Map<String, String> map = new HashMap<String, String>();
		map.put("flag", "true");
        return map;
    }
    
	/**
   	 * DownExcel
   	 * 下载附件excel
   	 */
    @RequestMapping(value = "/DownExeclBaseattach", method = RequestMethod.GET)
    public @ResponseBody Map<String, String> DownExeclBaseattach(HttpServletResponse res,HttpServletRequest req,String exceldata) throws Exception{
       Map<String, String> map = new HashMap<String, String>();
       	ExcelEntity excelEntity = new ExcelEntity();
       	//存放对应的表头名称和对应的数据库字段
    	Map<String, Object> titlemap = new LinkedHashMap<String, Object>();
    	//下载文件名
    	String fileName=".xls";
       	if(exceldata.contains("||") ){// 以"||"分割的字符串
	    	  List<String> arg=  StringUtil.splitString(exceldata, "||");
	    	  for(int i=0;i<arg.size();i++){
	    		  if(i==0){
	    			  fileName=arg.get(0)+fileName; 
	    		  }else{
	    			  String[] arg1=   arg.get(i).split(":"); 
	    			  titlemap.put(arg1[0], arg1[1]);
	    		  }
	    	  }
	    }
       
    	excelEntity.setFilename(fileName);
    	excelEntity.setLinename(titlemap);
    	//直接查询结果集的语句
    	String sql="select * from Baseattach where 1=1";
    	//eg:
		//if(StringUtil.isNotEmpty(menuname)){
		//	sql+=" and basemenu.menuname='"+menuname+"' ";
		//}
    	excelEntity.setSql(sql);
    	this.DownExecl(res, req, excelEntity);
    	map.put("flag", "true");
        return map;
    }
}
